import axios from 'axios'
import moment from 'moment'

// 老黄历
export class Almanac{
  private year: String | undefined
  private month: String | undefined
  private day: String | undefined
  private nongli: String | undefined // 农历
  private star: String | undefined // 星座
  private suici:Array<String> | undefined // 岁次
  private yi: Array<String> | undefined
  private ji: Array<String> | undefined
  
  // https://api.jisuapi.com/huangli/date?appkey=ca29254694e94896&year=2015&month=10&day=27
  public constructor(){
    this.updateAlmanac()
  }

  public async updateAlmanac(){
    
    let dateArray = moment().format("YYYY-MM-DD").split("-")
    this.year = dateArray[0]
    this.month = dateArray[1]
    this.day = dateArray[2]

    await axios.get(`https://api.jisuapi.com/huangli/date?appkey=ca29254694e94896&year=${this.year}&month=${this.month}&day=${this.day}`).then((response:any) => {
      
      let data = response.data.result
      console.log(data)  
      this.nongli = data.nongli
      this.star = data.star
      this.suici = data.suici
      this.yi = data.yi
      this.ji = data.ji      
    })
  }

  public getAlmanac(){
    return {year: this.year,
      month: this.month,
      day: this.day,
      nongli: this.nongli, // 农历
      star: this.star, // 星座
      suici: this.suici,// 岁次
      yi: this.yi,
      ji: this.ji
    }
  }
}